//
//  TestingCell.m
//  IpadTestB
//
//  Created by edianzu on 2017/7/6.
//  Copyright © 2017年 ruilai. All rights reserved.
//

#import "TestingCell.h"

@implementation TestingCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self createUI];
    }
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    return self;
}

-(void)createUI{
    self.contentView.backgroundColor =  UIColorFromRGB(241, 246, 248);
    
    
    self.backView = [[UIView alloc]init];
    self.backView.layer.cornerRadius = 5;
    self.backView.layer.masksToBounds = YES;
    self.backView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.backView];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).with.offset(-5*AutoScaleVertical);
    }];
    
    
    self.nameLabel = [[UILabel alloc]init];
    self.nameLabel.textColor = [UIColor blackColor];
    self.nameLabel.font = [UIFont boldSystemFontOfSize:20*AutoScaleHorizontal];
//    [self.nameLabel setTextAlignment:NSTextAlignmentCenter];
        self.nameLabel.text = @"睿小莱";
    [self.backView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backView.mas_left).with.offset(25*AutoScaleHorizontal);
        make.top.mas_equalTo(self.backView.mas_top).with.offset(15*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(80*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    
    self.sexView = [[UIImageView alloc]init];
    self.sexView.image = [UIImage imageNamed:@"manIMG"];
    [self.backView addSubview:self.sexView];
    [self.sexView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel.mas_right).with.offset(10*AutoScaleHorizontal);
        make.top.mas_equalTo(self.backView.mas_top).with.offset(15*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(20*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    
    self.ageLabel = [[UILabel alloc]init];
    self.ageLabel.textColor = [UIColor blackColor];
    self.ageLabel.font = [UIFont systemFontOfSize:18*AutoScaleHorizontal];
    //    [self.nameLabel setTextAlignment:NSTextAlignmentCenter];
        self.ageLabel.text = @"年龄:100岁";
    [self.backView addSubview:self.ageLabel];
    [self.ageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel.mas_left);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).with.offset(15*AutoScaleVertical);
        make.size.mas_equalTo(CGSizeMake(150*AutoScaleHorizontal, 20*AutoScaleVertical));
    }];
    
    self.resultLabel = [[UILabel alloc]init];
    self.resultLabel.textColor = [UIColor blackColor];
    self.resultLabel.font = [UIFont boldSystemFontOfSize:20*AutoScaleHorizontal];
        [self.resultLabel setTextAlignment:NSTextAlignmentRight];
        self.resultLabel.text = @"未测试";
    [self.backView addSubview:self.resultLabel];
    [self.resultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.backView.mas_right).with.offset(-35*AutoScaleHorizontal);
        make.centerY.mas_equalTo(self.backView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(300*AutoScaleHorizontal, 25*AutoScaleVertical));
    }];
    
    self.arrowView =[[UIImageView alloc]init];
    self.arrowView.image = [UIImage imageNamed:@"arrowImage"];
    [self.backView addSubview:self.arrowView];
    [self.arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.backView.mas_right).with.offset(-10*AutoScaleHorizontal);
        make.centerY.mas_equalTo(self.backView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(15*AutoScaleHorizontal, 15*AutoScaleVertical));
    }];
    
    
    
    self.coverBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.coverBtn.backgroundColor = [UIColor clearColor];
    [self.coverBtn addTarget:self action:@selector(pressCover) forControlEvents:UIControlEventTouchUpInside];
    [self.backView addSubview:self.coverBtn];
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(self.backView.mas_right);
//        make.centerY.mas_equalTo(self.backView.mas_centerY);
//        make.size.mas_equalTo(CGSizeMake(self.backView.width/2, self.backView.height));
        make.left.top.right.bottom.mas_equalTo(self.contentView);
    }];
}

-(void)pressCover{
    NSLog(@"cover");
    self.callinput();
}

-(void)relayoutCellWithDic:(NSMutableDictionary *)dataDictionary andProjectId:(NSNumber *)projectId{
    self.arrowView.hidden=NO;
      self.resultLabel.textColor = [UIColor blackColor];
    //对数据源解析
    ChildModel * thisModel = [ChildModel modelWithDictionary:[dataDictionary objectForKey:@"master"]];
    DetailListModel * thisDetailModel = [DetailListModel modelWithDictionary:[dataDictionary objectForKey:@"detailDic"]];
    //区分单位
    NSString * unit = [[NSString alloc]init];
    switch ([projectId integerValue]) {
        case 1:
            unit = @"厘米";
            break;
        case 2:
            unit = @"千克";
            break;
        case 4:
            unit = @"圈";
            break;
        case 5:
            unit = @"个";
            break;
        case 6:
            unit = @"";
            break;
        case 7:
            unit = @"个";
            break;
        case 8:
            unit = @"厘米";
            break;
        case 9:
            unit = @"厘米";
            break;
        case 10:
            unit = @"秒";
            break;
        default:
            break;
    }
    if ([projectId  isEqual: @8]) {
//        NSString * ing = @"-111";
        
        if (thisDetailModel.IsTest ==true) {
            self.resultLabel.text = @"无法测试";
        }else if ([thisDetailModel.Result isEqual:@-111] || thisDetailModel.Result == nil) {
            self.resultLabel.text = @"未测试";
            self.resultLabel.textColor =[UIColor redColor];
        }else{
            self.resultLabel.text = [NSString stringWithFormat:@"%@%@",thisDetailModel.Result,unit];
        }
    }else{
        if (thisDetailModel.IsTest ==true) {
            self.resultLabel.text = @"无法测试";
        }else if (thisDetailModel.Result == nil){
            self.resultLabel.text = @"未测试";
            self.resultLabel.textColor =[UIColor redColor];
        }else{
            self.resultLabel.text = [NSString stringWithFormat:@"%@%@",thisDetailModel.Result,unit];
            
        }
    }

    
    //不为20米和平衡木时， 按钮可选，字色变黑
    self.nameLabel.textColor = [UIColor blackColor];
    self.ageLabel.textColor = [UIColor blackColor];

    self.nameLabel.text =  thisModel.ChildName;
    self.coverBtn.userInteractionEnabled = YES;
        CGSize size = [thisModel.ChildName sizeWithAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:20.0]}];
        // ceilf()向上取整函数, 只要大于1就取整数2. floor()向下取整函数, 只要小于2就取整数1.
        CGSize sizee = CGSizeMake(ceilf(size.width), ceilf(size.height));
        [self.nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.backView.mas_left).with.offset(25*AutoScaleHorizontal);
            make.top.mas_equalTo(self.backView.mas_top).with.offset(15*AutoScaleVertical);
            make.size.mas_equalTo(CGSizeMake(sizee.width, sizee.height));
        }];
    
    
    NSString * birthStr = [thisModel.Birthday substringWithRange:NSMakeRange(0, 10)];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *birthDate = [dateFormatter dateFromString:birthStr];
    NSString * realAge = [self dateToDetailOld:birthDate];
    if ([realAge integerValue]<3) {
        self.ageLabel.text = [NSString stringWithFormat:@"年龄:3岁"];
    }else{
    self.ageLabel.text = [NSString stringWithFormat:@"年龄:%@岁",realAge];
    }
    if ([thisModel.Gender isEqual:@1]) {
        self.sexView.image = [UIImage imageNamed:@"manIMGS"];
    }else{
        self.sexView.image = [UIImage imageNamed:@"womenIMGS"];
    }
    
    
    //判断是否20米和平衡木
    if ([projectId isEqual: @4]) {
        if ([realAge integerValue] <= 8) {
            self.arrowView.hidden=YES;
            self.nameLabel.textColor = UIColorFromRGB(167, 167, 167);
            self.ageLabel.textColor = UIColorFromRGB(167, 167, 167);
            self.resultLabel.textColor = UIColorFromRGB(167, 167, 167);
            self.resultLabel.text = @"孩子不满9岁，请测试平衡木";
            self.coverBtn.userInteractionEnabled = NO;
            if ([thisModel.Gender isEqual:@1]) {
                self.sexView.image = [UIImage imageNamed:@"manIMG"];
            }else{
                self.sexView.image = [UIImage imageNamed:@"womenIMGS"];
            }
        }
    }
    if ([projectId isEqual: @10]) {
        if ([realAge integerValue] > 8) {
            self.arrowView.hidden=YES;
            self.nameLabel.textColor = UIColorFromRGB(167, 167, 167);
            self.ageLabel.textColor = UIColorFromRGB(167, 167, 167);
            self.resultLabel.textColor = UIColorFromRGB(167, 167, 167);
            self.resultLabel.text = @"孩子超过8岁，请测试15米折返跑";
            self.coverBtn.userInteractionEnabled = NO;
            if ([thisModel.Gender isEqual:@1]) {
                self.sexView.image = [UIImage imageNamed:@"manIMG"];
            }else{
                self.sexView.image = [UIImage imageNamed:@"womenIMGS"];
            }
        }
    }
}
@end


















